#include<unistd.h>
#include<fcntl.h>
#include<sys/mman.h>
#include<string.h>
#include<sys/stat.h>
#include<stdlib.h>
#include<stdio.h>
#include<errno.h>
int main(int argc,char** argv)
{

    int fd;
   typedef  struct student
   {
       int num;
       char *name;
       char sex;
       }stu;
    
    fd=open("consanguinity.txt",O_RDWR|O_CREAT|O_TRUNC);

    if(fd>0)
    {
        perror("open fail!\n");
        }

    //该进程向mmap区域写结构体
    stu st={20,"zhangsan",'M'};

    //向consanguinity.txt中进行扩容
    lseek(fd,40,SEEK_END);
    write(fd,"\0",1);

    int size=lseek(fd,0,SEEK_END);
     //创建映射区域
     stu *p=(stu*)mmap(NULL,size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
     if(p==MAP_FAILED)
     {
         
         perror("mmap fail!\n");

        }
     while(1)
     {
         memcpy(p,&st,sizeof(st));
         st.num++;
         sleep(1);
         }
        //向mmmap内存映射区域写结构体
       // memcpy(p,&st,sizeof(st));
     munmap(p,sizeof(st));
     
        close(fd);



return 0;
}
